﻿#ifndef CONTROLER_H
#define CONTROLER_H

#include <QWidget>
#include "cortrolset.h"
#include <QTimer>
#include "pidfuc.h"

namespace Ui {
class Controler;
}

class Controler : public QWidget
{
    Q_OBJECT

public:
    explicit Controler(QWidget *parent = nullptr);
    ~Controler();
    QString name = "GasControl";
    QString utilsType = "gas";

    void autoControl(QString cmd, QString value);
    void openAdmin(bool);

    static QString ip;
    static int gasInSwitchPort;
    static int gasOutSwitchPort;
    static int launchSwitchPort;
    static int gasAnalogPort;
    static float gasCoefficient;
    static float zero;
    static float default_ ;
    static int luanchTime ;

    static float kp;
    static float ki;
    static float kd;

    void loadSetDetails();
    void saveSetDetails();

    void readOutput();


    void setGasValue(float value);
    void setIp(QString ip, bool flag);

    void setMessageBox(QString);

    void setMoveFlag(bool flag);
    QTimer *moveFlagTimer = new QTimer;
    bool tempFlag = false;
    QString userfilePath = "admin";


private slots:
    void on_switch_in_clicked();

    void on_launch_clicked();

    void on_set_clicked();



    void on_switch_out_clicked();

    void on_pressure_limit_clicked(bool checked);

    void on_gasOut_clicked();

private:
    Ui::Controler *ui;
    cortrolSet *set = new cortrolSet;

    pidFuc *pid = new pidFuc;

    int time;

    bool openInFlag = false;
    bool openOutFlag = false;
    bool openLaunchFlag = false;

    bool autoCloseFlag = false;
    float targetPressure = 0;

    void automaticStabilization(float target,float current, float fault);

    QTimer *launchTimer = new QTimer;
    float currentValue;
    float historyValue;

    QTimer *inTimer = new QTimer;
    QTimer *outTimer = new QTimer;

    QTimer *autoInSwitch = new QTimer;
    bool autoInFlag = false;
    QTimer *autoOutSwitch = new QTimer;

    QTimer *timer = new QTimer;
    bool autoOutFlag = false;
    int autoControlTime = 5000;
    int okCount = 0;//稳定自动关闭

    bool turnOn(int gasSwitch);
    bool turnOff(int gasSwitch);

signals:
    void option(QString op);

};

#endif // CONTROLER_H
